package com.jbit.examSystem.controller;

import com.github.pagehelper.PageHelper;
import com.jbit.examSystem.param.PageParam;
import com.jbit.examSystem.pojo.*;
import com.jbit.examSystem.param.ExamSearchParam;
import com.jbit.examSystem.pojo.Class;
import com.jbit.examSystem.service.ClassService;
import com.jbit.examSystem.service.ExamService;
import com.jbit.examSystem.service.ScoreService;
import com.jbit.examSystem.service.TestPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("exam")
public class ExamController {
    @Autowired
    private ExamService examService;
    @Autowired
    private TestPaperService testPaperService;
    @Autowired
    private ClassService classService;
    @Autowired
    private ScoreService scoreService;

    @RequestMapping("add")
    public String  add(Model model){
        Integer i = null;
        List<Class> classes1 = new ArrayList<>();
        List<Class> classes2 = new ArrayList<>();
        List<Class> classes3 = new ArrayList<>();
        List<Class> classes = classService.getAllClass(i);
        for (Class aClass : classes) {
            if(aClass.getGradeId()==10000){
                classes1.add(aClass);
            }else if(aClass.getGradeId()==10001){
                classes2.add(aClass);
            }else if(aClass.getGradeId()==10002){
                classes3.add(aClass);
            }
        }
        model.addAttribute("classes1",classes1);
        model.addAttribute("classes2",classes2);
        model.addAttribute("classes3",classes3);

        return "onlineExam/exam/add";
    }

    @RequestMapping("getExamByClassId")
    @ResponseBody
    public TableDataInfo getExamByClassId(Integer classId){
        TableDataInfo respData = new TableDataInfo();

        try {
            List<Exam> exams = examService.getExamByClassId(classId);
            respData.succeed(exams, 0);
        } catch (Exception e){
            respData.setCode(500);
            e.printStackTrace();
        }

        return respData;
    }


    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo list(ExamSearchParam param, PageParam page){
        TableDataInfo respData = new TableDataInfo();
        try {
            PageHelper.startPage(page.getPageNum(), page.getPageSize(), true, true, false);
            List<Exam> exams = examService.list(param);
            respData.succeed(exams, 0);
        } catch (Exception e){
            respData.setCode(500);
            e.printStackTrace();
        }
        return respData;
    }

    @RequestMapping("addExam")
    @ResponseBody
    public Map<String, Object> addExam(ExamAdd examAdd){
        System.out.println(examAdd);
        Map<String, Object> map = new HashMap<>();
        try {
            //examService.addExam(examAdd);
            map.put("code", 0);
            map.put("msg", "考试添加成功");
        }catch (Exception e){
            map.put("code", 500);
            map.put("msg", "考试添加失败！原因是："+ e.getMessage());
        }

        return map;
    }

    @RequestMapping("createExamPage")
    public String createExamPage(Integer examId, Model model){
        Exam exam = examService.getExamById(examId);
        List<Question> questions = testPaperService.getQuestionByTestPaperId(exam.getTestPaper().getId());
        model.addAttribute("exam", exam);
        model.addAttribute("questions", questions);
        return "onlineExam/review/reviewExam";
    }

    @RequestMapping("unifiedTest")
    public String unifiedTest(Model model, HttpSession session){
        Student stu = (Student) session.getAttribute("user");
        List<Exam> exams = examService.getExamByClassId(stu.getClassId());
        exams.forEach(exam -> {
            Score score = scoreService.getScoreByExamIdAndStudentId(stu.getId(), exam.getId());
            exam.setIsJoin(!(score == null));
        });
        model.addAttribute("exams", exams);
        return "onlineExam/review/unifiedTest";
    }
}
